package com.example.smartcity.ui.selfCenter;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.example.smartcity.R;
import com.example.smartcity.ui.Bean.LoginBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SelfMessageSetC extends AppCompatActivity {


    private ImageView selfImage;
    private TextView selfName;
    private RadioButton RBMan;
    private RadioButton RBWoman;
    private EditText EDIDNumber;
    private EditText ETPhoneNumber;
    private Button OK;
    SharedPreferences sharedPreferences;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    Toast.makeText(SelfMessageSetC.this, msg.obj+"", Toast.LENGTH_SHORT).show();
                    setBack();
                    break;
            }
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_self_message_set_c);
        selfImage = (ImageView) findViewById(R.id.selfImage);
        selfName = (TextView) findViewById(R.id.selfName);
        RBMan = (RadioButton) findViewById(R.id.RB_man);
        RBWoman = (RadioButton) findViewById(R.id.RB_woman);
        EDIDNumber = (EditText) findViewById(R.id.ED_IDNumber);
        ETPhoneNumber = (EditText) findViewById(R.id.ET_phoneNumber);
        OK = (Button) findViewById(R.id.OK);
        initView();
        Change();
    }
    public void setBack(View view){
        this.finish();
    }
    public void setBack(){
        this.finish();
    }
    public void initView(){
        sharedPreferences= getSharedPreferences("selfMessage",MODE_PRIVATE);
        String userName = sharedPreferences.getString("userName","无用户名");
        String sex = sharedPreferences.getString("userSex","0");
        String userPhone = sharedPreferences.getString("userPhone","无");
        String IDCard = sharedPreferences.getString("IDCard","");
        String userPhoto = sharedPreferences.getString("userPhoto",null);
        selfName.setText(userName);
        if (Integer.valueOf(sex)==0){
            RBMan.setChecked(true);
        }else{
            RBWoman.setChecked(true);
        }
        EDIDNumber.setText(IDCard);
        ETPhoneNumber.setText(userPhone);
        if (userPhoto.length()>0){
            Glide.with(getBaseContext())
                    .load(OkHttpUtils.getBaseUrl(getBaseContext())+userPhoto)
                    .transform(new CircleCrop())
                    .into(selfImage);
        }
    }
    public void Change(){
        OK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String userId  = sharedPreferences.getString("userID","0");
                String newUrl = OkHttpUtils.getBaseUrl(getBaseContext())+"/system/user/updata";
                FormBody body = new FormBody.Builder()
                        //自己的ID
//                        .add("file")
                        .add("userId",userId)
                        .add("nickName",selfName.getText().toString())
                        .add("sex",RBMan.isChecked()?"0":"1")
                        .add("idCard",EDIDNumber.getText().toString())
                        .add("phonenumber",ETPhoneNumber.getText().toString()).build();
                OkHttpUtils.getCall(newUrl,body,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.d("TAG", "onFailure: "+e.getMessage());
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        getNewToken();
                        stateBean bean = new Gson().fromJson(response.body().string(),stateBean.class);
                        Message message = new Message();
                        message.what=1;
                        message.obj = bean.getMsg();
                        handler.sendMessage(message);
                    }
                });
            }
        });
    }
    public void getNewToken() throws IOException {
        sharedPreferences = getSharedPreferences("UwsandPws",MODE_PRIVATE);
        LoginBean loginBean = new LoginBean();
        loginBean.setUsername(sharedPreferences.getString("userName",null));
        loginBean.setPassword(sharedPreferences.getString("pws",null));
        Gson gson = new Gson();
        String json = gson.toJson(loginBean);
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json;charset=UTF-8"),json);
        String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/login";
        Response response = OkHttpUtils.getCall(newUrl,requestBody).execute();
        String BackJson = response.body().string();
        LoginBean loginBean1 = gson.fromJson(BackJson,LoginBean.class);
        String newToken = loginBean1.getToken();
        new File(getBaseContext()).saveFile("Token",newToken);
    }
}